//---------------------------------------------------------------------------

#include "vcl_stl.h"
#pragma hdrstop

#include "h_base.h"
#include "h_xml.h"
#include "h_global.h"
using namespace h_base;
using namespace h_xml;

#include "h_g.h"
#include "h_global.h"
#include "h_i18n.h"

#include "t_passwordform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TPasswordForm *PasswordForm;
//---------------------------------------------------------------------------
__fastcall TPasswordForm::TPasswordForm(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TPasswordForm::ButtonOkClick(TObject *Sender)
{
   OkFlag = true;
   Close();
}
//---------------------------------------------------------------------------

void __fastcall TPasswordForm::GetPassword(UTF8String &Id, UTF8String &Password)
{
   static int LastIdx = -1;
   static vector<HServerInfo> ServerVec;

   if ( LastIdx == -1 )
   {
      HXmlTree Config;
      if ( !Config.LoadFromFile((h_g::BinDir + "simbat.cfg").c_str()) )
      {
         char Err[200 + 1];
         snprintf(Err, 200, h_g::I18nString("t_passwordform:LOAD_FAIL"),
            "simbat.cfg", Config.GetErrorString().c_str());
         ShowMessage(UTF8String(Err));
         Id = "";
         Password = "";
         return;
      }
      vector<HXmlTree*> Nodes;
      Config.GetChildren("SERVER", Nodes);
      for ( int i = 0; i < Nodes.size(); i++ )
      {
         HXmlTree *Node = Nodes[i];
         HServerInfo ServerInfo;
         ServerInfo.Name = Node->GetValue("CAPTION").c_str();
         ServerInfo.IpAddr = Node->GetValue("IP_ADDR").c_str();
         ServerInfo.Port = Node->GetValue("PORT").c_str();
         ServerInfo.Protocol = Node->GetValue("PROTOCOL").c_str();
         if ( ServerInfo.Protocol == "" )
         {
            ServerInfo.Protocol = "0105";
         }
         ServerVec.push_back(ServerInfo);
      }
      if ( ServerVec.empty() )
      {
         ShowMessage(UTF8String(h_g::I18nString("t_passwordform:SERVER_NOT_DEFINED")));
         Id = "";
         Password = "";
         return;
      }
      ComboBoxServer->Items->Clear();
      for( int i = 0; i < ServerVec.size(); i++ )
      {
         UTF8String TmpStr;
         TmpStr.sprintf("%s (%s : %s)", ServerVec[i].Name.c_str(),
            ServerVec[i].IpAddr.c_str(), ServerVec[i].Port.c_str());
         ComboBoxServer->Items->Add(TmpStr);
      }
      ComboBoxServer->ItemIndex = 0;
   }

   UTF8String ErrorString;
   OkFlag = false;
   EditPassword->Text = "";
   ShowModal();

   if( OkFlag )
   {
      LastIdx = ComboBoxServer->ItemIndex;
      h_g::ServerIpAddr = ServerVec[LastIdx].IpAddr;
      h_g::ServerPort = ServerVec[LastIdx].Port.ToIntDef(0);
      h_g::ServerProtocol = ServerVec[LastIdx].Protocol;
      Id = EditId->Text.TrimRight();
      Password = EditPassword->Text.TrimRight();
   }
   else
   {
      Id = "";
      Password = "";
   }
}

void __fastcall TPasswordForm::FormShow(TObject *Sender)
{
   if ( EditId->Text == "" )
   {
      EditId->SetFocus();
   }
   else
   {
      EditPassword->SetFocus();
   }
}
//---------------------------------------------------------------------------

